﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon;
using CDMS.Common;
using CDMS.Entities;
using CDMS.BLL;
using CDMS.Utilities;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;

namespace CDMS.Forms
{
    public partial class frmContractInfo : XtraForm
    {
        Contract_BLL Contract_BLL;
        Contract _contract;
        int Contract_ID;

        public frmContractInfo(int Contract_ID)
        {
            InitializeComponent();

            this.DialogResult = DialogResult.Cancel;
            Contract_BLL = new Contract_BLL();

            LookUpEditDataSource.ContractStatus(lookUpEditTrangThaiHopDong);
            LookUpEditDataSource.ContractType(lookUpEditLoaiHopDong);
            LookUpEditDataSource.PaymentWay(lookUpEditCachThanhToan);

            AddColumn();

            this.Contract_ID = Contract_ID;
            if (Contract_ID > 0)
            {
                _contract = Contract_BLL.GetOne(Contract_ID);
                if (_contract != null)
                {
                    memoEditNoiDung.Text = _contract.Contract_Content;
                    lookUpEditTrangThaiHopDong.EditValue = _contract.Contract_ContractStatusID;
                    lookUpEditLoaiHopDong.EditValue = _contract.Contract_ContractTypeID;
                    
                    //Lấy thông tin của khách hàng
                    Customer_BLL cus = new Customer_BLL();
                    Customer ct = cus.GetOne(_contract.Contract_CustomerID);
                    searchControlKhachHang.Tag = _contract.Contract_CustomerID;
                    searchControlKhachHang.Text = ct.Customer_Name;
                    //

                    textEditDienGiai.Text = _contract.Contract_Description;
                    dateEditNgayBatDau.EditValue = _contract.Contract_EffectDate;
                    dateEditNgayKetThuc.EditValue = _contract.Contract_ExpireDate;
                    textEditSoHopDong.Text = _contract.Contract_No;
                    checkEditNhacViec.Checked = _contract.Contract_Remind;
                    dateEditNgayKy.EditValue = _contract.Contract_SignDate;
                    calcEditGiaTriHopDong.Value = _contract.Contract_Value;

                    textEditSoHopDong.Properties.ReadOnly = true;
                }
            }
            SetTitle(Contract_ID);
        }

        private void SetTitle(int iD)
        {
            lbTitle.Text = iD == 0 ? "Thêm " : "Sửa ";
            lbTitle.Text += "hợp đồng";
            this.Text = lbTitle.Text;
        }

        private void AddColumn()
        {
            //Hide row header GroupPanel
            gridViewLichThanhToan.OptionsView.ShowGroupPanel = false;
            //Hide row header
            gridViewLichThanhToan.OptionsView.ShowIndicator = false;
            gridViewLichThanhToan.Columns.Clear();

            FormGlobals.GridView_AddColumn(gridViewLichThanhToan, new GridColumn(), "ContractPaySchedule_Date", "Ngày thanh toán", "dd/MM/yyyy", 110, true, true);
            FormGlobals.GridView_AddColumn(gridViewLichThanhToan, new GridColumn(), "ContractPaySchedule_Amount", "Số tiền", "#,##0", 100, true, true);
            FormGlobals.GridView_AddColumn(gridViewLichThanhToan, new GridColumn(), "ContractPaySchedule_Rate", "Tỷ lệ", "#,##0.00", 80, true, true);
            FormGlobals.GridView_AddColumn(gridViewLichThanhToan, new GridColumn(), "ContractPaySchedule_Content", "Nội dung", "", 230, true, true);
            FormGlobals.GridView_AddColumn(gridViewLichThanhToan, new GridColumn(), "ContractPaySchedule_Remind", "Không nhắc", "", 100, true, true);

            //Hide row header GroupPanel
            gridViewHoaDonTheoHopDong.OptionsView.ShowGroupPanel = false;
            //Hide row header
            gridViewHoaDonTheoHopDong.OptionsView.ShowIndicator = false;
            gridViewHoaDonTheoHopDong.Columns.Clear();

            FormGlobals.GridView_AddColumn(gridViewHoaDonTheoHopDong, new GridColumn(), "Contract_Date", "Ngày", "dd/MM/yyyy", 90, true, true);
            FormGlobals.GridView_AddColumn(gridViewHoaDonTheoHopDong, new GridColumn(), "Contract_No", "Số hợp đồng", "", 150, true, true);
            FormGlobals.GridView_AddColumn(gridViewHoaDonTheoHopDong, new GridColumn(), "Contract_TotalAmount", "Tổng cộng", "#,##0", 100, true, true);
            FormGlobals.GridView_AddColumn(gridViewHoaDonTheoHopDong, new GridColumn(), "Contract_TotalPayment", "Đã thanh toán", "#,##0", 100, true, true);
        }

        bool CheckValid()
        {
            dxErrorProvider1.ClearErrors();



            //return dxValidationProvider1.Validate();
            return true;
        }
        void ErrorProvider_SetError(Control control, string errorText)
        {
            dxErrorProvider1.SetError(control, errorText);
            dxErrorProvider1.SetIconAlignment(control, ErrorIconAlignment.TopRight);
            //dxErrorProvider1.GetIconAlignment()
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!CheckValid())
                return;

            FormGlobals.ShowWaitForm();

            _contract = new Contract();
            _contract.Contract_Content = memoEditNoiDung.Text;
            _contract.Contract_ContractStatusID = Convert.ToInt32(lookUpEditTrangThaiHopDong.EditValue);
            _contract.Contract_ContractTypeID = Convert.ToInt32(lookUpEditLoaiHopDong.EditValue);
            _contract.Contract_CustomerID = Convert.ToInt32(searchControlKhachHang.Tag);
            _contract.Contract_Description = textEditDienGiai.Text;
            _contract.Contract_EffectDate = Convert.ToDateTime(dateEditNgayBatDau.EditValue);
            _contract.Contract_ExpireDate = Convert.ToDateTime(dateEditNgayKetThuc.EditValue);
            _contract.Contract_ID = Contract_ID;
            _contract.Contract_No = textEditSoHopDong.Text;
            _contract.Contract_Remind = checkEditNhacViec.Checked;
            _contract.Contract_SignDate = Convert.ToDateTime(dateEditNgayKy.EditValue);
            _contract.Contract_Value = Convert.ToDecimal(calcEditGiaTriHopDong.Value);


            if (Contract_BLL.Exists(_contract))
            {
                ErrorProvider_SetError(textEditSoHopDong, layoutControlItem1.Text + " đã tồn tại.");

                FormGlobals.Message_Warning(layoutControlItem1.Text + " đã tồn tại.", "Cảnh báo");
                return;
            }

            if (Contract_ID == 0)
            {
                Contract_ID = Contract_BLL.Insert(_contract);
                if (Contract_ID > 0)
                {
                    FormGlobals.Message_Information("Thêm thành công", "Thông báo");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                if (Contract_BLL.Update(_contract))
                {
                    FormGlobals.Message_Information("Sửa thành công", "Thông báo");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }
            FormGlobals.CloseWaitForm();
        }

        private void searchControlKhachHang_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == ButtonPredefines.Search)
            {
                frmCustomerSearch f = new frmCustomerSearch();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    searchControlKhachHang.Text = f.Customer.Customer_Name;
                    searchControlKhachHang.Tag = f.Customer.Customer_ID;
                }
            }
        }


    }
}